package com.neuedu.wemall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.wemall.common.vo.ResponseData;
import com.neuedu.wemall.service.vo.MenuPermissionVO;
import org.springframework.stereotype.Service;
import com.neuedu.wemall.dao.entity.Role;
import com.neuedu.wemall.service.IRoleService;
import com.neuedu.wemall.dao.mapper.RoleMapper;

import java.util.Date;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Override
    public ResponseData insertRole(Role role) {
        /**
         * 后台数据验证:判断角色中文名称和英文名称是否填写,角色数量上限是否小于0
         * 通常不需要验证,因为数据已在前端验证过了,这里仅仅做示范
         */
        /********后台数据验证开始*******/
        String roleNameZh = role.getNameCn();
        String roleNameEn = role.getNameEn();
        int maxCount = role.getMax();
        if (StrUtil.isEmpty(roleNameZh)) {
            return ResponseData.createFail( "角色中文名称未填写");
        }
        if (StrUtil.isEmpty(roleNameEn)) {

            return ResponseData.createFail( "角色英文名称未填写");
        }
        if (maxCount < 0) {
            return ResponseData.createFail( "角色数量上限不能小于0");
        }
        /********后台数据验证结束*******/
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getNameCn, roleNameZh);
        wrapper.or();
        wrapper.eq(Role::getNameEn, roleNameEn);
        //查询角色中文名称或英文名称是否被使用
        long count = getBaseMapper().selectCount(wrapper);
        if (count > 0) {
            return ResponseData.createFail("角色中文名称或角色中文名称重复");
        }
        role.setDelStatus(1);//使用状态默认为1
        role.setCreateTime(new Date());
        int rows = getBaseMapper().insert(role);
        return ResponseData.success(rows);
    }

    @Override
    public int updateRole(Role role) {
        return 0;
    }

    @Override
    public int deleteRole(int roleId) {
        return 0;
    }

    @Override
    public List<Role> selectRoleList(String keyword, Role role) {
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            lqw.like(Role::getNameCn, keyword);
            lqw.or();
            lqw.like(Role::getNameEn, keyword);
        }
        Integer delStatus = role.getDelStatus();
        if (delStatus != null && (delStatus == 1 || delStatus == 9)) {
            lqw.eq(Role::getDelStatus, delStatus);
        }
        return getBaseMapper().selectList(lqw);
    }

    @Override
    public Role selectById(int roleId) {
        return getBaseMapper().selectById(roleId);
    }

    @Override
    public List<Integer> selectRoleMenuList(int roleId) {
        return null;
    }

    @Override
    public int setRoleMenu(int roleId, List<Integer> menuIds) {
        return 0;
    }

    @Override
    public List<MenuPermissionVO> selectMenuPermission(int roleId) {
        return null;
    }
}